<template>
  <div>
    <!-- <van-pull-refresh v-model="pullLoading" @refresh="onRefresh"> -->
      <div class="search">
        <van-search
      :value="searchValue"
      use-action-slot
      placeholder="请输入队伍名称搜索"
      @clear="clearSearch()"
      @change="changeValue"
      @focus="auth"
      @scroll="scroll"
    >
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search>
      </div>
   
    <div class="fixed" @click="gotoPage('/pageTeam/createTeam')">
      <van-icon name="plus" size="40px" color="#fff" custom-style="line-height:60px" />
    </div>

    <TeamListCard :team-list="teamList" @getMore="getMore"></TeamListCard>
  </div>
  <van-notify id="van-notify" />
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { getTeamList, searchTeam } from '../../api/team/team'
import TeamListCard from '../../components/teamListCard.vue'
import {throttle} from '../../utils/tool'
import {onPullDownRefresh} from '@dcloudio/uni-app'
import {onShow} from '@dcloudio/uni-app'

const gotoPage = (pageRouter) => {
  uni.navigateTo({ url: pageRouter })
}
let teamList = ref([])
const teamPage = reactive({
  pageNum: 1,
  pageSize: 8,
})

const searchValue = ref('')

const loading = ref(false)

const pageSize = ref(4)
const pageNum = ref(1)
const getTeam = async () => {
  teamPage.pageNum=1
  uni.showLoading({
    title:'网络加载中...',
    mask:true
  })
  const res = await getTeamList(teamPage)
  teamList.value = res.data.records
  console.log(teamList.value)
  uni.hideLoading()
  uni.stopPullDownRefresh()
}
const changeValue = (e) => {
  console.log(e.detail)

  searchValue.value = e.detail
}
const clearSearch = () => {
  teamPage.pageNum = 1
  getTeam()
}
const onSearch = async (e) => {
  if(uni.getStorageSync("Authorization")===null){
    uni.showToast({
      title:'请登录',
      icon:'none'
    })
    uni.switchTab({
      url:'/pages/person/person'
    })
    return false
  }
  const res = await searchTeam({ name: searchValue.value })
  console.log(res)
  teamList.value = res.data.teamUserVOList
}
//触底加载
const getMore1=async ()=>{
  console.log('到底了');

  teamPage.pageNum++
  const res = await getTeamList(teamPage)
 let resArr = res.data.records
  teamList.value=[...teamList.value,...resArr]
  console.log(teamList.value)
 
//  userList.value=userList.value.push(res.data.records)
//  userList.value?.forEach((item) => {
//     item.tags = JSON.parse(item.tags)
//   })
}
//节流
const getMore=throttle(getMore1,1000)
const auth=()=>[
uni.addInterceptor('navigateTo', {
        invoke: (e) => {
            console.log(e);
            
            const excludeUrl = ['/pages/home/home', '/pages/team/team', '/pages/person/person']; // 不拦截的页面路由
            const index = excludeUrl.findIndex(item => item === e.url); // 检查当前URL是否在不拦截列表中
            const Authorization = uni.getStorageSync("Authorization");
            console.log("Authorization", Authorization);
            
            // 仅当用户未登录且页面不在排除列表时进行拦截
            if (Authorization === null && index === -1) {
                uni.switchTab({
                    url: "/pages/person/person",
                    success: () => {
                        uni.showToast({
                            title: '请先登录',
                            icon: 'none'
                        });
                    }
                });
                // 阻止进一步的跳转操作
                return false;
            }
            console.log("跳转前拦截", e);
        }
    })
]
onMounted(() => {
  // getTeam()
})
onShow(()=>{
  getTeam()
})
onPullDownRefresh(()=>{
  getTeam()
})
</script>
<style scoped>
  .fixed{
    position: fixed;
    top: 80%;
    right: 8px;
    width: 60px;
    height: 60px;
    background-color: #1989fa;
    border-radius: 50%;
    z-index: 9999;
    text-align: center;
    
    
  }
  .search{
    padding-right: 8px;
  }
</style>
